$(document).ready(function() {
/*    $('#docAttachmentCell1error').hide();
    $('#bodyAttachmenterror').hide();
    $('#docTypeCell1error').hide();
    $('#receivedBmsCell1error').hide();*/
    $(".datepickerReq").datepicker({ dateFormat: 'dd/mm/y'});
    $(".ocultos").each(function(i,v){
        $(v).hide();
    });
});

function validate(url) {
   jQuery.validator.addMethod("valueNotEquals", function(value, element, arg){
        return value != arg;
    });

    $("#formEdit")
        .validate(
        {
            rules : {
                'safetydocs.droga.id': {
                    required: true,
                    valueNotEquals: "-1"
                },
                'paisesElegidos': {
                    required: true,
                    valueNotEquals: "-1" },
                'rolesElegidos': {
                    required: true,
                    valueNotEquals: "-1" }
            },

            messages : {
                'safetydocs.droga.id': {
                    valueNotEquals: "*"
                },
                'paisesElegidos' : {
                    valueNotEquals: "*"
                },
                'rolesElegidos' : {
                    valueNotEquals: "*"
                }
            },
            submitHandler: function(form) {
                findMails(url,$('#droga').val(), $('#countries').val().join(','), $('#roles').val().join(',') );
            }
        })

/*    var completos = true;
    $(".req").each(function(index,value){
        var idCell = value.id + "error";
        if(value.value == ""){
            $("#"+idCell).show();
            $("#"+idCell).parent().show();
            $(value).css("border-color", "red");
            if ($('#'+idCell+':hidden') == false )
                completos = false;
        }
        else{
            $("#"+idCell).parent().hide();
            $(value).css("border", "1px inset gray");
        }
    });

    $(".datepickerReq").each(function(index,value){
        var idCell = value.id + "error";
        if(value.value == ""){
            $("#"+idCell).show();
            $("#"+idCell).parent().show();
            $(value).css("border-color", "red");
            if ($('#'+idCell+':hidden') == false )
                completos = false;
        }
        else{
            $("#"+idCell).parent().hide();
            $(value).css("border", "1px inset gray");
        }
    });

    $(".fileClass").each(function(index,value){
        var idCell = value.id + "error";
        if(value.value == ""){
            $("#"+idCell).show();
            if ($('#'+idCell+':hidden') == false )
                completos = false;
        }
        else{
            $("#"+idCell).hide();
        }
    });*/

    var completos = true;
    $(".req").each(function(index,value){
        var idCell = value.id + "error";
        if ($(value).is(":hidden") == false ){
            $(value).rules("add", {
                required:true
            });
        }
        else{
            $(value).rules("remove");
        }
    });

    $(".datepickerReq").each(function(index,value){
        var idCell = value.id + "error";
        if ($(value).is(":hidden") == false ){
            $(value).rules("add", {
            required:true
        });
        }
        else{
            $(value).rules("remove");
        }
    });

    $(".fileClass").each(function(index,value){
        var idCell = value.id + "error";
        if ($(value).is(":hidden") == false ){
            $(value).rules("add", {
            required:true
        });
        }
        else{
            $(value).rules("remove");
        }
    });

    return ($("#formEdit").valid() && completos);

}

function removeDoc(tableID,fila){
    var table = document.getElementById(tableID);
    var cant = document.getElementById("cant");

    var rows = table.getElementsByTagName('tr');
    var size = rows.length;
    for(var j = 0; j < rows.length; j++ ){
        if(rows[j].id == fila[0].id && rows[j].cells[1].id == fila[0].cells[1].id){
            break;
        }
    }

    if(cant.value == 1){
        var borrados = 0;
        var x = j - 1;
        while(borrados < 12){
            if(rows[x].cells[1] != null){
                rows[x].cells[1].children[0].value="";
                borrados++;
            }
            x--;
        }
        return;
    }

    var borrados = 0;
    var x = j;
    while(borrados < 13){
        rows[x].parentNode.removeChild(rows[x]);
        if(rows[x].cells[1] != null){
            borrados++;
        }
        x--;
    }

    var val = parseInt(cant.value,10) - parseInt(1,10);
    cant.value = val;
/*
    var table = document.getElementById(tableID);
    var cant = document.getElementById("cant");

    var rows = table.getElementsByTagName('tr');
    var size = rows.length;
    for(var j = 0; j < rows.length; j++ ){
        if(rows[j].id == fila[0].id && rows[j].cells[0].id == fila[0].cells[0].id){
            break;
        }
    }

    if(cant.value == 1){
        for ( var x = j - 1; x >= (j - 2); x --){
            rows[x].cells[1].children[0].value="";
        }
        rows[x].cells[1].childNodes[0].value = "";
        return;
    }


    for ( var x = j; x >= (j - 3); x --){
        rows[x].parentNode.removeChild(rows[x]);
    }

    var val = parseInt(cant.value,10) - parseInt(1,10);
    cant.value = val;*/
}

function addDoc(tableID) {

        var table = document.getElementById(tableID);

        var rowCount = table.rows.length;

        var cant = document.getElementById("cant");
        var val = parseInt(cant.value,10) + parseInt(1,10);
        cant.value = val;

        var ultimoId = document.getElementById("ultimoId");

        for(var k = ultimoId.value; k>0; k--){
            if (document.getElementById("cell" + k) != null)
                break;
        }

        var newId = parseInt(ultimoId.value,10) + parseInt(1,10);
        ultimoId.value = newId;

        var idAfter = '#cell' + k;

        var row12 = $('#cell' + k).parent().clone()[0];
        row12.cells[1].id="cell"+newId;
       $(idAfter).parent().after(row12);

        var row11 = $('#docAttachmentCell' + k).parent().parent().clone()[0];
        row11.cells[1].children[0].name="files";
        row11.cells[1].children[0].id="docAttachmentCell"+newId;
        row11.cells[1].children[0].value="";
        if(row11.cells[1].children.length == 2)
            row11.cells[1].children[1].parentNode.removeChild(row11.cells[1].children[1]);
        $(idAfter).parent().after(row11);

        var row10 = $('#docCommentCell' + k).parent().parent().clone()[0];
        row10.cells[1].children[0].name="safetydocs.envioses["+newId+"].comment";
        row10.cells[1].children[0].id="docCommentCell"+newId;
        row10.cells[1].children[0].value="";
        $(idAfter).parent().after(row10);

        var row9 = $('#replaceDateCell' + k).parent().parent().clone()[0];
        row9.cells[1].children[0].name="safetydocs.envioses["+newId+"].replaceDate";
        row9.cells[1].children[0].id="replaceDateCell"+newId;
        row9.cells[1].children[0].value="";
        row9.id="docTypeCell" + newId + "replaceDate";
        $(row9).hide();
        $(idAfter).parent().after(row9);

        var row8 = $('#replaceNumberCell' + k).parent().parent().clone()[0];
        row8.cells[1].children[0].name="safetydocs.envioses["+newId+"].replaceNumber";
        row8.cells[1].children[0].id="replaceNumberCell"+newId;
        row8.cells[1].children[0].value="";
        row8.id="docTypeCell" + newId + "replaceNumber";
        $(row8).hide();
        $(idAfter).parent().after(row8);

        var row7 = $('#cutoffDateCell' + k).parent().parent().clone()[0];
        row7.cells[1].children[0].name="safetydocs.envioses["+newId+"].cutoffDate";
        row7.cells[1].children[0].id="cutoffDateCell"+newId;
        row7.cells[1].children[0].value="";
        if(row7.cells[1].children.length == 2)
           row7.cells[1].children[1].parentNode.removeChild(row7.cells[1].children[1]);
        row7.id="docTypeCell" + newId + "cutoffDate";
        $(row7).hide();
        $(idAfter).parent().after(row7);

        var row6 = $('#versionDateCell' + k).parent().parent().clone()[0];
        row6.cells[1].children[0].name="safetydocs.envioses["+newId+"].versionDate";
        row6.cells[1].children[0].id="versionDateCell"+newId;
        row6.cells[1].children[0].value="";
          if(row6.cells[1].children.length == 2)
           row6.cells[1].children[1].parentNode.removeChild(row6.cells[1].children[1]);
        row6.id="docTypeCell" + newId + "versionDate";
        $(row6).hide();
        $(idAfter).parent().after(row6);

        var row5 = $('#controlNumberCell' + k).parent().parent().clone()[0];
        row5.cells[1].children[0].name="safetydocs.envioses["+newId+"].controlNumber";
        row5.cells[1].children[0].id="controlNumberCell"+newId;
        row5.cells[1].children[0].value="";
          if(row5.cells[1].children.length == 2)
            row5.cells[1].children[1].parentNode.removeChild(row5.cells[1].children[1]);
        row5.id="docTypeCell" + newId + "controlNumber";
        $(row5).hide();
        $(idAfter).parent().after(row5);

        var row4 = $('#processedDateCell' + k).parent().parent().clone()[0];
        row4.cells[1].children[0].name="safetydocs.envioses["+newId+"].processedDate";
        row4.cells[1].children[0].id="processedDateCell"+newId;
        row4.cells[1].children[0].value="";
        if(row4.cells[1].children.length == 2)
            row4.cells[1].children[1].parentNode.removeChild(row4.cells[1].children[1]);
        row4.id="docTypeCell" + newId + "processedDate";
        $(row4).hide();
        $(idAfter).parent().after(row4);

        var row3 = $('#revisionDateCell' + k).parent().parent().clone()[0];
        row3.cells[1].children[0].name="safetydocs.envioses["+newId+"].revisionDate";
        row3.cells[1].children[0].id="revisionDateCell"+newId;
        row3.cells[1].children[0].value="";
         if(row3.cells[1].children.length == 2)
          row3.cells[1].children[1].parentNode.removeChild(row3.cells[1].children[1]);
        row3.id="docTypeCell" + newId + "revisionDate";
        $(row3).hide();
        $(idAfter).parent().after(row3);

        var row2 = $('#eventCell' + k).parent().parent().clone()[0];
        row2.cells[1].children[0].name="safetydocs.envioses["+newId+"].event";
        row2.cells[1].children[0].id="eventCell"+newId;
        row2.cells[1].children[0].value="";
        if(row2.cells[1].children.length == 2)
            row2.cells[1].children[1].parentNode.removeChild(row2.cells[1].children[1]);
        row2.id="docTypeCell" + newId + "event";
        $(row2).hide();
        $(idAfter).parent().after(row2);

        var row1 = $('#receivedBmsCell' + k).parent().parent().clone()[0];
        row1.cells[1].children[0].name="safetydocs.envioses["+newId+"].receivedBms";
        row1.cells[1].children[0].id="receivedBmsCell"+newId;
        row1.cells[1].children[0].value="";
        if(row1.cells[1].children.length == 2)
            row1.cells[1].children[1].parentNode.removeChild(row1.cells[1].children[1]);
        $(idAfter).parent().after(row1);

        var row0 = $('#docTypeCell' + k).parent().parent().clone()[0];
        row0.cells[1].children[0].name="safetydocs.envioses["+newId+"].type";
        row0.cells[1].children[0].id="docTypeCell"+newId;
        row0.cells[1].children[0].value="";
     if(row0.cells[1].children.length == 2)
         row0.cells[1].children[1].parentNode.removeChild(row0.cells[1].children[1]);
        $(idAfter).parent().after(row0);
}

function findMails(url, droga, paises, roles){
    windowCaller("E-mail recipients", url, { "drogaBuscar" : droga, "paisesBuscar": paises, "rolesBuscar": roles });
}

function sendMail(){
    $.window.closeAll();
    document.getElementById('formEdit').submit();
}

function cancelMail(){
    $.window.closeAll();
}

function verSafety(url, id){
    windowCaller("View", url, { "idVer": id });
}

function closeSafety(){
    $.window.closeAll();
}

function validateTracking() {

    $("#formEdit")
        .validate(
        {
            rules : {

                'mailId': {
                    number: true
                }
            }
        })
}

function updateDatepickers(){
    $('.datepicker').removeClass('hasDatepicker').datepicker({ dateFormat: 'dd/mm/y'});
    $('.datepickerReq').removeClass('hasDatepicker').datepicker({ dateFormat: 'dd/mm/y'});

}

function showTypeFields(select){
    var id = select.id;
    var type = $("#"+id+" option:selected").text();
    if( type == "IB"){
        showIB(id);
    }
    else if( type == "SUSAR"){
        showSusar(id);
    }
    else if( type == "Addendum"){
        showAddendum(id);
    }
    else{
        hideAll(id);
    }
}

function hideAll(id){
    $("#" + id + "versionDate").hide();
    $("#" + id + "versionDate").val('');

    $("#" + id + "cutoffDate").hide();
    $("#" + id + "cutoffDate").val('');

    $("#" + id + "replaceNumber").hide();
    $("#" + id + "replaceNumber").val('');

    $("#" + id + "replaceDate").hide();
    $("#" + id + "replaceDate").val('');

    $("#" + id + "event").hide();
    $("#" + id + "event").val('');

    $("#" + id + "revisionDate").hide();
    $("#" + id + "revisionDate").val('');

    $("#" + id + "processedDate").hide();
    $("#" + id + "processedDate").val('');

    $("#" + id + "controlNumber").hide();
    $("#" + id + "controlNumber").val('');
}

function showSusar(id){
    $("#" + id + "versionDate").hide();
    $("#" + id + "versionDate").val('');

    $("#" + id + "cutoffDate").hide();
    $("#" + id + "cutoffDate").val('');

    $("#" + id + "replaceNumber").hide();
    $("#" + id + "replaceNumber").val('');

    $("#" + id + "replaceDate").hide();
    $("#" + id + "replaceDate").val('');

    $("#" + id + "event").show();
/*    if($($($("#" + id + "event").children()[1]).children()[0]).css("border-color") == "rgb(255, 0, 0)" ){
        $("#" + id + "event").show();
        $($("#" + id + "event").children()[2]).show();
    }
    else
        $($("#" + id + "event").children()[2]).hide();*/

    $("#" + id + "revisionDate").show();
/*    if($($($("#" + id + "revisionDate").children()[1]).children()[0]).css("border-color") == "rgb(255, 0, 0)" ){
        $("#" + id + "revisionDate").show();
        $($("#" + id + "revisionDate").children()[2]).show();
    }
    else
        $($("#" + id + "revisionDate").children()[2]).hide();*/

    $("#" + id + "processedDate").show();
/*    if($($($("#" + id + "processedDate").children()[1]).children()[0]).css("border-color") == "rgb(255, 0, 0)" ){
        $("#" + id + "processedDate").show();
        $($("#" + id + "processedDate").children()[2]).show();
    }
    else
        $($("#" + id + "processedDate").children()[2]).hide();*/

    $("#" + id + "controlNumber").show();
/*    if($($($("#" + id + "controlNumber").children()[1]).children()[0]).css("border-color") == "rgb(255, 0, 0)" ){
        $("#" + id + "controlNumber").show();
        $($("#" + id + "controlNumber").children()[2]).show();
    }
    else
        $($("#" + id + "controlNumber").children()[2]).hide();*/
}

function showIB(id){
    $("#" + id + "versionDate").show();
/*    if( $($($("#" + id + "versionDate").children()[1]).children()[0]).css("border-color") == "rgb(255, 0, 0)" ){
        $("#" + id + "versionDate").show();
        $($("#" + id + "versionDate").children()[2]).show();
    }
    else
        $($("#" + id + "versionDate").children()[2]).hide();*/

    $("#" + id + "cutoffDate").show();
 /*   if($($($("#" + id + "cutoffDate").children()[1]).children()[0]).css("border-color") == "rgb(255, 0, 0)" ){
        $("#" + id + "cutoffDate").show();
        $($("#" + id + "cutoffDate").children()[2]).show();
    }
    else
        $($("#" + id + "cutoffDate").children()[2]).hide();*/

    $("#" + id + "replaceNumber").show();
    $("#" + id + "replaceDate").show();

    $("#" + id + "event").hide();
    $("#" + id + "event").val('');

    $("#" + id + "revisionDate").hide();
    $("#" + id + "revisionDate").val('');

    $("#" + id + "processedDate").hide();
    $("#" + id + "processedDate").val('');

    $("#" + id + "controlNumber").hide();
    $("#" + id + "controlNumber").val('');
}

function showAddendum(id){
    $("#" + id + "versionDate").show();
/*    if( $($($("#" + id + "versionDate").children()[1]).children()[0]).css("border-color") == "rgb(255, 0, 0)" ){
        $("#" + id + "versionDate").show();
        $($("#" + id + "versionDate").children()[2]).show();
    }
    else
        $($("#" + id + "versionDate").children()[2]).hide();*/

    $("#" + id + "cutoffDate").hide();
    $("#" + id + "cutoffDate").val('');

    $("#" + id + "replaceNumber").hide();
    $("#" + id + "replaceNumber").val('');

    $("#" + id + "replaceDate").hide();
    $("#" + id + "replaceDate").val('');

    $("#" + id + "event").hide();
    $("#" + id + "event").val('');

    $("#" + id + "revisionDate").hide();
    $("#" + id + "revisionDate").val('');

    $("#" + id + "processedDate").hide();
    $("#" + id + "processedDate").val('');

    $("#" + id + "controlNumber").hide();
    $("#" + id + "controlNumber").val('');
}